package org.coody.framework.minicat.util;

import java.net.URLEncoder;
import java.security.MessageDigest;

import org.coody.framework.minicat.config.MiniCatConfig;

public final class EncryptUtil {

    public final static String CHARSET = "UTF-8";

    private EncryptUtil() {
        throw new Error("Utility classes should not instantiated!");
    }

    public static String md5Code(String pwd) {
        try {
            MessageDigest md = MessageDigest.getInstance("MD5");
            md.update(pwd.getBytes(MiniCatConfig.ENCODE));
            byte b[] = md.digest();

            int i;

            StringBuffer buf = new StringBuffer("");
            for (int offset = 0; offset < b.length; offset++) {
                i = b[offset];
                if (i < 0) {
                    i += 256;
                }
                if (i < 16) {
                    buf.append("0");
                }
                buf.append(Integer.toHexString(i));
            }
            return buf.toString();

        } catch (Exception e) {

        }
        return "";
    }

    private static String textCode(String s, String key) {
        String str = "";
        int ch;
        if (key.length() == 0) {
            return s;
        } else if (!s.equals(null)) {
            for (int i = 0, j = 0; i < s.length(); i++, j++) {
                if (j > key.length() - 1) {
                    j = j % key.length();
                }
                ch = s.codePointAt(i) + key.codePointAt(j);
                if (ch > 65535) {
                    ch = ch % 65535;// ch - 33 = (ch - 33) % 95 ;
                }
                str += (char) ch;
            }
        }
        return str;

    }

    @SuppressWarnings("unused")
    private static String textDeCode(String s, String key) {
        String str = "";
        int ch;
        if (key.length() == 0) {
            return s;
        }
        if (!s.equals(key)) {
            for (int i = 0, j = 0; i < s.length(); i++, j++) {
                if (j > key.length() - 1) {
                    j = j % key.length();
                }
                ch = (s.codePointAt(i) + 65535 - key.codePointAt(j));
                if (ch > 65535) {
                    ch = ch % 65535;// ch - 33 = (ch - 33) % 95 ;
                }
                str += (char) ch;
            }
        }
        return str;
    }

    public static String customEnCode(String str) {
        try {
            str = md5Code(str);
            str = textCode(str, str);
            str = str.substring(1, str.length() - 1);
            str = URLEncoder.encode(str, "UTF-8").replace("%", "").toLowerCase();
            str = md5Code(str);
            return str;
        } catch (Exception e) {
            return "";
        }

    }

}
